#include "main.h"   



int start_lescan(int device_id)
{
    int device_handle = 0;

    if((device_handle = hci_open_dev(device_id)) < 0)
    {
        perror("Could not open device");
        return -1;
    }
    uint8_t filter_policy = 0x00;
    uint16_t interval = htobs(0x0010);
    uint16_t window = htobs(0x0010);

    int err = hci_le_set_scan_parameters(device_handle, 0x01, interval, window, 0x00, filter_policy, 1000);
    if (err < 0)
    {
        perror("Set scan parameters failed");
        return -1;
    }

    printf("Start scan BLE...\n");

    err = hci_le_set_scan_enable(device_handle, 0x01, 0, 1000);
    if (err < 0)
    {
        perror("Enable scan failed");
        return -1;
    }
    return device_handle;
}



int stop_lescan(int device_handle)
{
    uint8_t filter_dup = 0;

    printf("Stop scan BLE...\n");

    int err = hci_le_set_scan_enable(device_handle, 0x00, filter_dup, 1000);
    if (err < 0)
    {
        perror("Disable scan failed");
    	//hci_close_dev(device_handle);

        return -1;
    }
    //hci_close_dev(device_handle);
    return 0;
}

